/*
package com.fm.magic.net.http;

import com.fm.base.param.HttpProxy;
import com.fm.commons.net.http.HttpClient;
import com.fm.commons.net.http.HttpMethod;
import com.fm.magic.weapon.legendary.MantaStyle;
import org.apache.http.NameValuePair;
import org.apache.http.entity.ContentType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;

public class ProxiesHttpClient extends HttpClient {

	private static volatile AtomicInteger index = new AtomicInteger(0);
	private static volatile ArrayList<HttpProxy> proxypool = new ArrayList<>();
	private static volatile ReentrantReadWriteLock lock = new ReentrantReadWriteLock(false);
	private static volatile MantaStyle.MirrorImage _images;

	private boolean lazyMode;

	public ProxiesHttpClient() {
		_images = new MantaStyle().active(null).accurate(false);
		lock.writeLock().lock();
		if (proxypool.isEmpty()) {
			reloadProxyList();
		} else {
			System.out.println("pool size:" + proxypool.size());
		}
		lock.writeLock().unlock();
	}

	public ProxiesHttpClient(MantaStyle.MirrorImage images) {
		_images = images;
		lock.writeLock().lock();
		if (proxypool.isEmpty()) {
			reloadProxyList();
		} else {
			System.out.println("pool size:" + proxypool.size());
		}
		lock.writeLock().unlock();
	}

	protected void changeProxy() {
		lock.readLock().lock();
		int i = index.incrementAndGet() % proxypool.size();
		HttpProxy proxy = proxypool.get(i);
		if (index.get() < 0) {
			index.set(0);
		}
		super.setProxy(proxy.host, proxy.port);
		lock.readLock().unlock();
	}

	@Override
	protected void prepare(HttpMethod method, String uri, Map<String, String> extraHeaders, Map<String, ?> data) {
		if (proxy == null || (!lazyMode && (lastStatus != 302))) {
			changeProxy();
		}
		super.prepare(method, uri, extraHeaders, data);
	}

	@Override
	protected void prepare(HttpMethod method, String uri, Map<String, String> extraHeaders, List<NameValuePair> data) {
		if (proxy == null || (!lazyMode && (lastStatus != 302))) {
			changeProxy();
		}
		super.prepare(method, uri, extraHeaders, data);
	}

	@Override
	protected void prepare(String uri, Map<String, String> extraHeaders, String data, ContentType contentType) {
		if (proxy == null || (!lazyMode && (lastStatus != 302))) {
			changeProxy();
		}
		super.prepare(uri, extraHeaders, data, contentType);
	}

	public void removeCurrentProxy() {
		lock.writeLock().lock();
		proxypool.remove(proxy);
		if (proxypool.isEmpty()) {
			reloadProxyList();
		} else {
			System.out.println("remain:" + proxypool.size());
		}
		lock.writeLock().unlock();
		proxy = null;
	}

	public boolean getLazyMode() {
		return lazyMode;
	}

	public void setLazyMode(boolean lazy) {
		lazyMode = lazy;
	}

	*/
/**
	 * 该方法必须在写锁开启的情况下使用
	 *//*


	protected void reloadProxyList() {
		if (!lock.isWriteLocked()) {
			throw new RuntimeException("write lock not open");
		}
		proxypool.addAll(_images.befall().stream().collect(Collectors.toList()));
		if (proxypool.isEmpty()) {
			throw new RuntimeException("no useful proxy in dataset");
		}
		System.out.println("load proxy list form db, count: " + proxypool.size());
	}
}

*/
